package com.wwu.system.service.impl;

import com.wwu.common.redis.RedisConst;
import com.wwu.common.redis.RedisUtil;
import com.wwu.common.utils.AssertUtil;
import com.wwu.common.web.ResultPage;
import com.wwu.system.entity.Redis;
import com.wwu.system.service.IRedisService;
import com.wwu.system.service.ISysConfigService;
import com.wwu.system.service.ISysDictService;
import com.wwu.system.service.ISysParamService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author 一蓑烟雨
 * @version 1.0.0
 * @date 2023-07-21 11:19
 */
@Service
public class RedisServiceImpl implements IRedisService {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ISysConfigService sysConfigService;
    @Resource
    private ISysParamService sysParamService;
    @Resource
    private ISysDictService sysDictService;

    /**
     * 分页查询缓存信息
     * @param pageNum  页码
     * @param pageSize  每页数量
     * @param redisKey   缓存键前缀
     * @param redisType   缓存键类型
     * @return com.wwu.common.web.ResultPage<com.wwu.monitor.entity.Redis>
     * @author 一蓑烟雨
     * @date 2023/7/21 11:20
     */
    @Override
    public ResultPage<Redis> getRedisListPage(Integer pageNum, Integer pageSize, String redisKey, String redisType) {
        Set<String> keys = redisUtil.getKeys(!StringUtils.hasText(redisKey) ? "" : redisKey);
        List<Redis> redisList = new ArrayList<>();
        if(!ObjectUtils.isEmpty(keys)){
            Redis redis = null;
            for (String key: keys) {
                redis = new Redis();
                redis.setKey(key);
                redis.setValue(String.valueOf(redisUtil.getValue(key)));
                redis.setExpire(redisUtil.getExpire(key));
                if(key.equals(RedisConst.Key.SYS_DICT)||key.equals(RedisConst.Key.SYS_CONFIG)||key.equals(RedisConst.Key.SYS_PARAM)){
                    redis.setType("0");
                }else {
                    redis.setType("1");
                }

                if(!StringUtils.hasText(redisType)|| redisType.equals(redis.getType())){
                    redisList.add(redis);
                }

            }
        }

        return new ResultPage<>(redisList.size(),redisList);
    }

    /**
     * 新增或更新缓存信息
     * @param redis redis实体类
     * @author 一蓑烟雨
     * @date 2023/7/21 11:20
     */
    @Override
    public void saveOrUpdateRedis(Redis redis) {
        AssertUtil.isTrue(!StringUtils.hasText(redis.getKey()),"缓存key不能为空");
        AssertUtil.isTrue(ObjectUtils.isEmpty(redis.getValue()),"缓存值不能为空");

        if(!ObjectUtils.isEmpty(redis.getExpire()) && -1 != redis.getExpire()){
            redisUtil.setValue(redis.getKey(), redis.getValue(), redis.getExpire());
        }else {
            redisUtil.setValue(redis.getKey(),redis.getValue());
        }
    }

    /**
     * 根据缓存key批量刷新内置缓存信息
     * @param redisKeys redis缓存键集合
     * @author 一蓑烟雨
     * @date 2023/7/21 11:20
     */
    @Override
    public void updateRedisByKeys(List<String> redisKeys) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(redisKeys),"缓存key不能为空");
        //加载系统字典缓存
        if(redisKeys.contains(RedisConst.Key.SYS_DICT)){
            sysDictService.sysDictCache();
        }
        //加载系统配置缓存
        if(redisKeys.contains(RedisConst.Key.SYS_CONFIG)){
            sysConfigService.sysConfigCache();
        }
        //加载系统参数缓存
        if(redisKeys.contains(RedisConst.Key.SYS_PARAM)){
            sysParamService.sysParamCache();
        }
    }

    /**
     * 删除缓存信息
     * @param redisKeys redis缓存键集合
     * @author 一蓑烟雨
     * @date 2023/7/21 11:20
     */
    @Override
    public void deleteRedis(List<String> redisKeys) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(redisKeys),"请选择删除的记录！");
        //删除缓存信息
        AssertUtil.isTrue(redisUtil.delKeys(redisKeys) != redisKeys.size(),"缓存删除失败！");
    }
}
